package iso3.pt.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import iso3.pt.model.Alumno;
import iso3.pt.model.Asignatura;
import iso3.pt.model.Profesor;
import iso3.pt.service.PtDaoService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

public class LecturerAction extends ActionSupport implements Preparable
{
	private static final long serialVersionUID = 1L;
	private int subjectID;
	private String subjectName;
	private Profesor profesor;
	private int studentDNI; 
	private List<Asignatura> listaAsignaturas;
	private List<Alumno> listaAlumnos;
	
//get y sets-------------------------------------------------------
	
	public Profesor getProfesor() {
		return profesor;
	}

	public void setProfesor(Profesor profesor) {
		this.profesor = profesor;
	}

	public List<Asignatura> getListaAsignaturas() {
		return listaAsignaturas;
	}

	public void setListaAsignaturas(List<Asignatura> listaAsignaturas) {
		this.listaAsignaturas = listaAsignaturas;
	}
	
	public int getSubjectID() {
		return subjectID;
	}

	public void setSubjectID(int subjectID) {
		this.subjectID = subjectID;
	}

	public List<Alumno> getListaAlumnos() {
		return listaAlumnos;
	}

	public void setListaAlumnos(List<Alumno> listaAlumnos) {
		this.listaAlumnos = listaAlumnos;
	}
	
	public int getStudentDNI() {
		return studentDNI;
	}

	public void setStudentDNI(int studentDNI) {
		this.studentDNI = studentDNI;
	}
	
	public String getSubjectName() {
		return subjectName;
	}

	public void setSubjectName(String subjectName) {
		this.subjectName = subjectName;
	}
	
// FIN get y sets-------------------------------------------------------
	
	public void prepare() throws Exception
	{

		this.listaAsignaturas = new ArrayList<Asignatura>();		
		PtDaoService hibernateDAO = new PtDaoService();
		
		try
		{
			if(this.listaAsignaturas.size() == 0)
			{
				this.profesor = (Profesor)ActionContext.getContext().getSession().get("logged-in");	
				
				for (Iterator<Asignatura> i = hibernateDAO.getAsignaturasProfesor(this.profesor.getId()).iterator(); i.hasNext();)
				{
					Asignatura asig = i.next();
					this.listaAsignaturas.add(asig);
				}
			}
		}catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
	}

// do's----------------------------------------------------	
	
	public String doListSubjects()
	{
		return SUCCESS;
	}

	public String doLogout()
	{
		//logout
		@SuppressWarnings("rawtypes")
		Map session = ActionContext.getContext().getSession();
	  
	  	if (session.get("logged-in") != null)
	  		session.remove("logged-in"); 
		
		return "logout";
	}
	
	public String doListStudents()
	{
		this.listaAlumnos = new ArrayList<Alumno>();
		PtDaoService hibernateDAO = new PtDaoService();
		this.subjectName = hibernateDAO.getAsignatura(subjectID).getNombre();
		
		try
		{
			if(this.profesor != null)
			{	
				for (Iterator<Alumno> i = hibernateDAO.getAlumnos(subjectID).iterator(); i.hasNext();)
				{
					Alumno alum = i.next();
					this.listaAlumnos.add(alum);
				}
			}
		}catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
		
		return "studentList";
	}
	
	public String doCancelStudentList()
	{
		return "backStudentList";
	}
	
	public String doAddMark()
	{
		return "addMark";
	}	
	
// FIN do's----------------------------------------------------	
	
}
	